package com.fcb.pois.util;

import com.fcb.pois.config.AliyunSample;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname CreateExcelUtil
 * @Description TODO
 * @Date 2021-11-29 18:13
 * @Created by Huang Minds
 */
public class CreateExcelUtil {
    /**
     * <h5>描述:从阿里云下载的Excel表默认无绑定关系ID,根据阿里云提供的接口查询绑定关系ID并在Excel数据的最后一列进行追加</h5>
     *
     * @param rootPath Excel文件所在路径
     * @param excelName Excel文件名称
     * @return
     */
    public static void addBindIdToExcel(String rootPath, String excelName, String subIdName) throws Exception {
        // "D:/putout/FC100000133260732.xlsx"
        List<List<String>> ss =  ReadExcelUtil.readExcelInfo(rootPath+"/"+excelName);
        List<String> map1 = ss.get(0);
        map1.add(ConstantUtil.ALIYUN_BIND_ID);
        int size = ss.size();
        for(int i =1 ;i<size;i++){
            List<String> map = ss.get(i);
            String phone = map.get(1);
            String subId = AliyunSample.getSubId(phone);
            map.add(subId);
        }
        PoiUtil.doWriteSubId(ss,rootPath,subIdName);
    }
    public static void mergeBindIdToExcel(String rootPath, String subName, String hiddenNumberName, String targetName) throws Exception {
        // /FC100000133260732_subId.xlsx
        List<List<String>> listBindingId = ReadExcelUtil.readExcelInfo(rootPath+"/"+subName);
        List<List<String>> listSmallAssociation = ReadExcelUtil.readExcelInfo(rootPath+"/"+hiddenNumberName);
        //添加标题
        listSmallAssociation.get(0).add(ConstantUtil.ALIYUN_BIND_ID);

        for(int i=1;i<listSmallAssociation.size();i++){
            List<String> mapa = listSmallAssociation.get(i);
            String phone = mapa.get(1);
            for(int j = 1; j< listBindingId.size(); j++){
                List<String> maps = listBindingId.get(j);
                String phones = maps.get(1);
                if (phone.equals(phones)){
                    String subId = maps.get(maps.size() - 1);
                    mapa.add(subId);
                }


            }
        }
        PoiUtil.doWriteMerge(listSmallAssociation,rootPath,targetName);
    }
    
    
    
    public static void addUserIdToExcel(String rootPath, String mergeExcel, String personExcel, String targetName) throws Exception {
        List<List<String>> listSaasUserData = ReadExcelUtil.readExcelInfo(rootPath+"/"+personExcel);
        List<List<String>> listTarget1 = ReadExcelUtil.readExcelInfo(rootPath+"/"+mergeExcel);
        //添加标题
        listTarget1.get(0).add(ConstantUtil.SAAS_USER_ID);

        for(int i = 1; i< listTarget1.size(); i++){
            List<String> mapa = listTarget1.get(i);
            int cellLength = mapa.size();
            if (ConstantUtil.CELL_MAX_LENGTH < cellLength){
                ConstantUtil.CELL_MAX_LENGTH = cellLength;
            }
            String phone = mapa.get(2);
            List<List<String>> finds = new ArrayList();
            for(int j = 1 ;j < listSaasUserData.size();j++){
                List<String> maps = listSaasUserData.get(j);
                String phones = maps.get(1);
                String[] ps = phones.split(",");
                if(ps.length>=2){
                    for (int x=0;x<ps.length;x++){
                        String vale = ps[x];
                        if (phone.equals(vale)){
                            finds.add(maps);
                            break;
                        }
                    }
                }else{
                    if (phone.equals(phones)){
                        finds.add(maps);
                    }
                }
                if(!ObjectUtils.isEmpty(finds)){
                    if(finds.size()>=2){
                        for(int x = 0;x<finds.size();x++){
                            List<String> findss = finds.get(x);
                            String FSTATUS = findss.get(2);
                            if("ENABLE".equals(FSTATUS)){
                                mapa.add(findss.get(0));
                                break;
                            }
                        }
                        break;
                    }else {
                        List<String> findss = finds.get(0);
                        if (ConstantUtil.CELL_MAX_LENGTH - mapa.size() > 0 ){
                            mapa.add("");
                        }
                        mapa.add(findss.get(0));
                        break;
                    }
                }
            }
        }
        PoiUtil.doWriteMergeLast(listTarget1,rootPath,targetName);
    }
}
